package com.hyacinth.cloudnote.common.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.domain.Auditable;
import org.springframework.lang.Nullable;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

@NoArgsConstructor
@Setter
@Getter
@ToString(callSuper = true)
public abstract class AbstractAuditableDTO extends AbstractPersistableDTO implements Auditable<UUID, UUID, LocalDateTime> {

    private static final long serialVersionUID = -2952810860228350235L;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人", position = 100)
    @JsonProperty(index = 100)
    private UUID creator;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", position = 101)
    @JsonProperty(index = 101)
    private LocalDateTime createdTime;

    /**
     * 修改人
     */
    @ApiModelProperty(value = "修改人", position = 102)
    @JsonProperty(index = 102)
    private UUID modifier;

    /**
     * 修改时间
     */
    @ApiModelProperty(value = "修改时间", position = 103)
    @JsonProperty(index = 103)
    private LocalDateTime modifiedTime;

    //////////////////////////////////////////////////
    /// Auditable
    //////////////////////////////////////////////////

    @Override
    @Nullable
    @JsonIgnore
    public Optional<UUID> getCreatedBy() {
        return Optional.ofNullable(this.creator);
    }

    @JsonIgnore
    @Override
    public final void setCreatedBy(final @Nullable UUID createdBy) {
        this.creator = createdBy;
    }

    @JsonIgnore
    @Override
    @Nullable
    public Optional<LocalDateTime> getCreatedDate() {
        return Optional.ofNullable(this.createdTime);
    }

    @JsonIgnore
    @Override
    public final void setCreatedDate(final @Nullable LocalDateTime createdDate) {
        this.createdTime = createdDate;
    }

    @JsonIgnore
    @Override
    @Nullable
    public Optional<UUID> getLastModifiedBy() {
        return Optional.ofNullable(this.modifier);
    }

    @JsonIgnore
    @Override
    public final void setLastModifiedBy(final @Nullable UUID lastModifiedBy) {
        this.modifier = lastModifiedBy;
    }

    @JsonIgnore
    @Override
    @Nullable
    public Optional<LocalDateTime> getLastModifiedDate() {
        return Optional.ofNullable(this.modifiedTime);
    }

    @JsonIgnore
    @Override
    public final void setLastModifiedDate(final @Nullable LocalDateTime lastModifiedDate) {
        this.modifiedTime = lastModifiedDate;
    }
}
